class Reference < ActiveRecord::Base
  attr_accessible :name, :composite_name, :binding_type, :description, :location, :location_abstract 
  
  def self.references_collect(composite_soa)
    comp_name =composite_soa.name
    composite_soa.references.collect do |ref|
      ref_name =ref.attribute('name').value
      reference = Reference.where(name: ref_name, composite_name: comp_name).first
      if reference.nil?
	reference = Reference.new
	reference.name = ref_name
	reference.composite_name = comp_name
	fill_fields(reference)
      end

      reference.binding_type = composite_soa.binding_type(ref)
      reference.location_abstract = composite_soa.wsdlLocation_abstract(ref)

      location = composite_soa.location(ref)
      if !location.nil? and (reference.location.nil? or  reference.location.empty?)
	reference.location = location
      elsif reference.location.nil?
	reference.location = ''
      end
      reference.save if reference.new_record? or reference.changed?
      reference
    end
  end
  
  def self.fill_fields(reference)
    if reference.description.nil? or reference.description.length == 0
      ref = Reference.find(:all, :conditions => "name = '#{reference.name}' and description <> ''").first
      reference.description = ref.description if !ref.nil?
    end
    if reference.location.nil? or reference.location.length == 0
      ref = Reference.find(:all, :conditions => "name = '#{reference.name}' and location <> ''").first
      reference.location = ref.location if !ref.nil?
    end
  end
  
end
